//
//  DoubleRecordVC.m
//  record_plugin
//
//  Created by zqj on 2024/8/20.
//

#import "DoubleRecordVC.h"
#import <avd_sdk/avd_sdk.h>

@interface DoubleRecordVC ()<AVDEngineDelegate,AVDRoomJoinDelegate,AVDVideoDelegate,AVDRoomDelegate>

@property (strong,nonatomic)AVDVideoView *bigRender;/**视频渲染窗口*/
@property (strong,nonatomic)AVDVideoView *smallRender;/**视频渲染窗口*/
@property (nonatomic, strong)AVDRoom *mRoom;/**房间模块*/
@property (nonatomic, strong)AVDVideo *mVideo;/**视频模块*/
@property (nonatomic, strong)AVDAudio *mAudio;/**音频模块*/
@property (nonatomic, strong)AVDScreen *mScreen;/**屏幕共享模块*/
@property (nonatomic, strong)AVDWhiteboard *mboard;/**白板模块*/
@property (nonatomic, strong)AVDCanvasView *canvasView;/**白板渲染*/
@property (nonatomic, strong)AVDWhiteboardInfo *boardInfo;/**白板信息*/
@property (nonatomic, strong) AVDChat *mchat;/**聊天模块*/
@property(nonatomic, strong)AVDUserManager *mUser;/**用户模块*/
@property (weak, nonatomic) IBOutlet UIButton *closeBtn;
@property (weak, nonatomic) IBOutlet UIView *bgView;

@end

@implementation DoubleRecordVC


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)dealloc{
    NSLog(@"DoubleRecordVC ==== dealloc");
}


- (void)viewDidLoad {
    [super viewDidLoad];
    //[self joinRoom:self.roomID];
    [self setupUI];
    [self configMoudle];

}

//发送振铃消息
- (void)sendRingMsg{
   NSDictionary *ringDic =  @{
       @"notify_content":@{
        @"NickName":self.nickName,
        @"auditid":@0,
        @"businessName":@"",
        @"businessid":@0,
        @"customerid":self.customerId,
        @"deviceType":@"iOSApp",
        @"productid":self.productId,
        @"phoneNumb":self.phone,
        @"userType":@"0",
        @"reserve":@0,
        @"_applyId":self.applyId,//@"202409251340176618"
        @"_frontCheckFaceScore":self.checkScore
    },
      @"notify_type":@(1)
   };
    NSData *ringData = [[NSJSONSerialization dataWithJSONObject:ringDic options:kNilOptions error:nil] base64EncodedDataWithOptions:0];
    [self.mRoom sendPublicData:ringData];
}

- (void)setupUI{
    self.bigRender = [[AVDVideoView alloc]initWithFrame:self.view.bounds];
    
    [self.bigRender setScalingType:Scale_Aspect_Full];
    //self.bigRender.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.bigRender];
    [self.view bringSubviewToFront:self.bgView];
    self.smallRender = [[AVDVideoView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame) - 190, 60, 140, 220)];
    //self.smallRender.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.smallRender];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(switchVideo)];
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(switchVideo2)];

    [self.smallRender addGestureRecognizer:tap];
    [self.bigRender addGestureRecognizer:tap2];
    [self.smallRender setScalingType:Scale_Aspect_Full];
    [self.view bringSubviewToFront:self.closeBtn];
}

- (void)switchVideo{
    CGRect smallFrame = self.smallRender.frame;
    CGRect bigFrame = self.bigRender.frame;
    self.bigRender.frame = smallFrame;
    self.smallRender.frame = bigFrame;

    if (smallFrame.size.width > bigFrame.size.width) {
        [self.view bringSubviewToFront:self.bigRender];
        [self.view bringSubviewToFront:self.smallRender];
    }else{
        [self.view bringSubviewToFront:self.smallRender];
        [self.view bringSubviewToFront:self.bigRender];
    }
    [self.view bringSubviewToFront:self.closeBtn];


}

- (void)switchVideo2{
    [self switchVideo];
}

- (void)configMoudle{
    self.mRoom = [AVDRoom obtain:self.roomID];
    self.mRoom.delegate = self;
    self.mAudio = [AVDAudio getAudio:self.mRoom];
    self.mVideo = [AVDVideo getVideo:self.mRoom];
    self.mVideo.delegate = self;
    self.mScreen = [AVDScreen getScreen:self.mRoom];
    self.mboard = [AVDWhiteboard getWhiteboard:self.mRoom];
    self.mchat = [AVDChat getChat:self.mRoom];
    self.mUser = [AVDUserManager getUserManager:self.mRoom];
    [self sendRingMsg];
}

- (IBAction)closeRoomClicked:(UIButton *)sender {
    NSDictionary *ringDic =  @{
        @"content":@{
         @"name":self.nickName,
         @"phone":self.phone,
     },
       @"cmd":@(401),
       @"type":@"hangUp"
    };
     NSData *ringData = [[NSJSONSerialization dataWithJSONObject:ringDic options:kNilOptions error:nil] base64EncodedDataWithOptions:0];
     [self.mRoom sendPublicData:ringData];
    [self.mRoom close];
    [self.mRoom destory];
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}


#pragma mark RoomDelegate
- (void)onPublicData:(NSData *)data fromUser:(AVDUserId)fromId{
    NSData *data2 = [[NSData alloc]initWithBase64EncodedData:data options:0];
    NSError *error = nil;
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:&error];
    NSString *pData = [[NSString alloc]initWithData:data2 encoding:NSUTF8StringEncoding];

    NSInteger cmd = [dictionary[@"cmd"] intValue];
    if (cmd == 200) {//坐席接听了
        [self.mVideo publishLocalCamera];
        [self.mAudio openMicrophone];
    }else if (cmd == 400){
        [self.mRoom close];
        [self.mRoom destory];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    NSLog(@"accept string = %@",pData);
}


#pragma mark videoDelegate
- (void)onPublishLocalResult:(AVDResult)result deviceId:(AVDDeviceId)fromId{
    [self.smallRender setVideoId:fromId];
    [self.mVideo attachRenderWithDeviceId:fromId render:self.smallRender];
}

- (void)onPublishCameraNotify:(AVDCamera *)camera{
    self.bgView.hidden = YES;
    [self.mVideo subscribe:camera.id];
    [self.bigRender setVideoId:camera.id];
    [self.mVideo attachRenderWithDeviceId:camera.id render:self.bigRender];
}

- (void)onUnpublishCameraNotify:(AVDCamera *)camera{
    self.bgView.hidden = NO;
    [self.mVideo unsubscribe:camera.id];
    [self.bigRender setVideoId:@""];
    [self.mVideo detachRenderWithRender:self.bigRender];
}

-(void)onLeaveIndication:(AVDResult)reason fromUser:(AVDUserId)fromId{
    NSArray *vcs = self.navigationController.viewControllers;
    NSLog(@"当前栈有%ld个VC",vcs.count);
    [self.navigationController popToViewController:vcs[vcs.count - 3] animated:YES];
    [self.mRoom destory];
}

@end
